#include<stdio.h>
#include<pthread.h>
pthread_rwlock_t lock;
int buf=0;
void* w_thread(void *arg)
{
	while(1)
	{
		if(pthread_rwlock_wrlock(&lock)!=0)
		{
			printf("reader is reading now,please waiting...\n");
			sleep(1);
		}
		else{
			buf++;
			printf("write %d\n:",buf);
			pthread_rwlock_unlock(&lock);
		}
		sleep(1);
	}
}

void *r_thread(void *arg)
{
	while(1)
	{
		if(pthread_rwlock_rdlock(&lock)!=0)
		{
			printf("writer is writing now,please waiting...\n");
		}
		else{
			printf("read data:%d\n",buf);
			pthread_rwlock_unlock(&lock);
		}
	}
}



int main()
{
	pthread_rwlock_init(&lock,NULL);
	pthread_t w_id,r_id;
	pthread_create(&w_id,NULL,w_thread,NULL);
	pthread_create(&r_id,NULL,r_thread,NULL);
	pthread_join(w_id,NULL);
	pthread_join(r_id,NULL);
	pthread_rwlock_destroy(&lock);

}
